//
//  MainViewController.swift
//  Weibo
//
//  Created by mac on 15/12/12.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    //必选的属性
    let mainTabBar = MainTabBar()
    
    //private func 是 swift中函数的特殊的声明形式,希望swift方法选择器 + 访问控制关键字 能够兼容 需要使用@objc
    //@objc private 要保证监听方法的安全性 并且可以响应点击事件的特殊写法
    @objc private func composeBtnDidClick(){
        print(__FUNCTION__)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        //tabbar 是只读性, 不能够直接设置 可以使用KVC  在运行时间接赋值(改变身份)
        
        print(tabBar.classForCoder)
        
       //监听加号按钮的点击事件
        mainTabBar.composeBtn.addTarget(self, action: "composeBtnDidClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        setValue(mainTabBar, forKey: "tabBar")
        //classForCoder 对象类的字符串形成
        print(tabBar.classForCoder)
        //添加子视图控制器
        addChildViewControllers()
        
    }
    
    //private 表示只能在本类中进行访问
    private func
        //Extra argument "title" in call
        
        addChildViewControllers() {
        
            
            
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息", imageName:"tabbar_message_center")
        addChildViewController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
    }
    //将添加子控制器抽调成方法重复利用
    private func addChildViewController(vc: UIViewController,title: String, imageName: String) {

        //设置子视图为根控制器
        let nav=UINavigationController(rootViewController: vc)
        //            title = "首页"//不能显示
        //            nav.title = "首页"//显示在下面
        vc.title = title
        //设置图片
        vc.tabBarItem.image = UIImage(named: imageName)
        //设置图片点击时的高亮状态,并渲染称为系统的原始图层显示出来
//        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
//        //设置下面的文字标题的高亮状态下的颜色
//        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: .Selected)//UIControlState.Selected枚举可以省略
        //高亮状态设置的简便方法
        self.tabBar.tintColor = UIColor.orangeColor()
        //在swift中 self 可以省略
        self.addChildViewController(nav)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
